/*
 * Date         Author        Note
 * 2015.04.16   wzwz          起底板参数
 * 2015.05.25   wzwz          增加剪夹联动
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "mh/common.h"
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "board_param.h"
#include "com.h"
#include "mhlabel.h"
#include "belt.h"
#include "keypane.h"
#include "info.h"
#include "board_test.h"


/*
 *
 */
static void CreateBoardParamInterface(HWND hWnd)
{
    HWND hTmp = -1;
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_BOARD_TEST_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_BOARD_TEST_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_BOARD_TEST_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);
    const char *sc[30] = {
            GetString(528), GetString(529), GetString(530), GetString(531), GetString(532),
            GetString(533), GetString(534), GetString(535), GetString(536), GetString(537),
            GetString(538), GetString(539), GetString(540), GetString(541), GetString(542),
            GetString(543), GetString(544), GetString(545), GetString(546), GetString(547),
            GetString(743), GetString(744), GetString(745), GetString(746), GetString(747),
            GetString(748), GetString(752), GetString(753), GetString(769), GetString(777),
    };
    char edit[30][32] = {{0}};
    sprintf(edit[0], "%d", GetBoardParamHighPos(g_knit_data));
    sprintf(edit[1], "%d", GetBoardParamSafePos(g_knit_data));
    sprintf(edit[2], "%d", GetBoardParamRollClosePos(g_knit_data));
    sprintf(edit[3], "%d", GetBoardParamRollHookOpenPos(g_knit_data));
    sprintf(edit[4], "%d", GetBoardParamHookOpenDelay(g_knit_data));
    sprintf(edit[5], "%d", GetBoardParamUpQuickPos(g_knit_data));
    sprintf(edit[6], "%d", GetBoardParamUpSlowPos(g_knit_data));
    sprintf(edit[7], "%d", GetBoardParamUpWaitPos(g_knit_data));
    sprintf(edit[8], "%d", GetBoardParamDownSlowPos(g_knit_data));
    sprintf(edit[9], "%d", GetBoardParamUpStartSpeed(g_knit_data));
    sprintf(edit[10], "%d", GetBoardParamUpQuickSpeed(g_knit_data));
    sprintf(edit[11], "%d", GetBoardParamUpSlowSpeed(g_knit_data));
    sprintf(edit[12], "%d", GetBoardParamToSafeSpeed(g_knit_data));

    sprintf(edit[13], "%d", GetBoardParamResetSpeed(g_knit_data));
    sprintf(edit[14], "%d", GetBoardParamDownSlowSpeed(g_knit_data));
    sprintf(edit[15], "%d", GetBoardParamDownHookOpenSpeed(g_knit_data));
    sprintf(edit[16], "%d", GetBoardParamDropRollSpeed(g_knit_data));
    sprintf(edit[17], "%d", GetBoardParamDropWaitTime(g_knit_data));
    sprintf(edit[18], "%d", GetBoardParamBalance(g_knit_data));
    sprintf(edit[19], "%d", GetBoardParamScissorTimes(g_knit_data));
    sprintf(edit[20], "%d", GetYarnLeftOut(g_knit_data));
    sprintf(edit[21], "%d", GetYarnRightOut(g_knit_data));
    sprintf(edit[22], "%d", GetBoardParamRabHookOpenPos(g_knit_data));
    sprintf(edit[23], "%d", GetBoardParamRabClosePos(g_knit_data));
    sprintf(edit[24], "%d", GetRabCloseSpeed(g_knit_data));
    sprintf(edit[25], "%d", GetRabOpenSpeed(g_knit_data));

    sprintf(edit[26], "%u", GetBoardParamGauze(g_knit_data));
    sprintf(edit[27], "%d", GetBoardParamKnitMode(g_knit_data));
    sprintf(edit[28], "%u", GetBoardParamPull(g_knit_data));
    strcat(edit[29], GetBoardParamCSSyn(g_knit_data) ? GetString(671) : GetString(672));
    int i = 0;
    for(i = 0; i < 30; ++i)
    {
        hTmp = CreateWindow(MH_LABEL_NAME,
            sc[i],
            WS_CHILD | WS_VISIBLE | BS_LEFT,
            IDC_STATIC,
            15 + (i / 13) * 235,
            60 + (i % 13) * 28,
            i > 25 ? 80 : 160,
            20,
            hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
        EnableWindow(hTmp, FALSE);

        CreateWindow(MH_EDIT_NAME,
            edit[i],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,

            (i > 25 ? 100 : 180) + (i / 13) * 235,
            60 + (i % 13) * 28,
            60,
            20,
            hWnd, 0);
    }
    CreateWindow(MH_LABEL_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_BOARD,
        550,
        400,
        60,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_CYAN);
}
/*
 * set edit value
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    int value = atoi(sc);
    switch(nID)
    {
    case IDC_EDIT1:
        SetBoardParamHighPos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 1:
        SetBoardParamSafePos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 2:
        SetBoardParamRollClosePos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 3:
        SetBoardParamRollHookOpenPos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 4:
        SetBoardParamHookOpenDelay(g_knit_data, value);
        break;
    case IDC_EDIT1 + 5:
        SetBoardParamUpQuickPos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 6:
        SetBoardParamUpSlowPos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 7:
        SetBoardParamUpWaitPos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 8:
        SetBoardParamDownSlowPos(g_knit_data, value);
        break;
    case IDC_EDIT1 + 9:
        {
            SetBoardParamUpStartSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 10:
        {
            SetBoardParamUpQuickSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 11:
        {
            SetBoardParamUpSlowSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 12:
        {
            SetBoardParamToSafeSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 13:
        {
            SetBoardParamResetSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 14:
        {
            SetBoardParamDownSlowSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 15:
        {
            SetBoardParamDownHookOpenSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 16:
        {
            SetBoardParamDropRollSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 17:
        {
            SetBoardParamDropWaitTime(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 18:
        {
            SetBoardParamBalance(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 19:
        {
            SetBoardParamScissorTimes(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 20:
        {
            SetYarnLeftOut(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 21:
        {
            SetYarnRightOut(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 22:
        {
            SetBoardParamRabHookOpenPos(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 23:
        {
            SetBoardParamRabClosePos(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 24:
        {
            SetRabCloseSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 25:
        {
            SetRabOpenSpeed(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 26:
        {
            SetBoardParamGauze(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 27:
        {
            SetBoardParamKnitMode(g_knit_data, value);
        }
        break;
    case IDC_EDIT1 + 28:
        {
            SetBoardParamPull(g_knit_data, value);
        }
        break;
    }
    if(data.pwnd_board_param)
    {
        data.pwnd_board_param->change.sys = TRUE;
    }
}
/*
 *
 */
static void DoEvent(HWND hWnd, ushort nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    switch(nID)
    {
    case IDC_EDIT1:
        if(data.pwnd_board_param)
        {
            SetBoardParamHighPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 1:
        if(data.pwnd_board_param)
        {
            SetBoardParamSafePos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 2:
        if(data.pwnd_board_param)
        {
            SetBoardParamRollClosePos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 3:
        if(data.pwnd_board_param)
        {
            SetBoardParamRollHookOpenPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 4:
        if(data.pwnd_board_param)
        {
            SetBoardParamHookOpenDelay(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 5:
        if(data.pwnd_board_param)
        {
            SetBoardParamUpQuickPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 6:
        if(data.pwnd_board_param)
        {
            SetBoardParamUpSlowPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 7:
        if(data.pwnd_board_param)
        {
            SetBoardParamUpWaitPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 8:
        if(data.pwnd_board_param)
        {
            SetBoardParamDownSlowPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 22:
        {
            SetBoardParamRabHookOpenPos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 23:
        {
            SetBoardParamRabClosePos(g_knit_data, data.pwnd_board_param->pulse);
        }
        break;
    case IDC_EDIT1 + 29:
        if(data.pwnd_board_param)
        {
            uchar n = GetBoardParamCSSyn(g_knit_data);
            n = !n;
            SetBoardParamCSSyn(g_knit_data, n);
            const char *sc;
            sc = n ? GetString(671) : GetString(672);

            HWND hTmp = GetDlgItem(hWnd, nID);
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);

            data.pwnd_board_param->change.sys = TRUE;
        }
        break;
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_board_param->change.all)
            {
                int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
                if(ret)
                {
                    printf("write param: %d\n", ret);
                    AfxInfoBox(hWnd, GetString(392), GetString(481));
                }
                else
                {
                    if(data.pwnd_board_param->change.sys)
                    {
                        SendSysParam(g_knit_data);
                    }
                }
                SendBoardParam(g_knit_data);
            }
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case (IDC_EDIT1 + 9)...(IDC_EDIT1 + 21):
    case (IDC_EDIT1 + 24):
    case (IDC_EDIT1 + 25):
    case IDC_EDIT1 + 26:
    case IDC_EDIT1 + 27:
    case IDC_EDIT1 + 28:
        {
            RECT rect;
            HWND hTmp = GetDlgItem(hWnd, nID);
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            int min = 0;
            int max = 100;
            if(nID == IDC_EDIT1 + 20 || nID == IDC_EDIT1 + 21)
            {
                min = 0;
                max = 1000;
            }
            else if(nID == IDC_EDIT1 + 26)
            {
                min = 1;
                max = 8;
            }
            else if(nID == IDC_EDIT1 + 27)
            {
                min = 1;
                max = 3;
            }
            CreateKeyboardWindow(hWnd, rect, min, max, atoi(GetWindowCaption(hTmp)));
        }
        break;
    case IDC_EDIT1 + 29:
        break;
    default:
        {
            RECT rect;
            HWND hTmp = GetDlgItem(hWnd, nID);
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            CreateKeyboardWindow(hWnd, rect, -10000, 10000, atoi(GetWindowCaption(hTmp)));
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 + 9 && nID < IDC_EDIT1 + 20)
        {
            nID -= 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 10)
        {
            nID += 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID < IDC_EDIT1 + 20)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 19)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_ENTER)
    {
        HWND hTmp = GetFocusChild(hWnd);
        ushort nID = GetDlgCtrlID(hTmp);
        /*if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 9)
        {
            DoEvent(hWnd, nID);
        }
        else if(nID == IDC_EDIT1 + 22 || nID == IDC_EDIT1 + 23)
        {
            DoEvent(hWnd, nID);
        }
        else if(nID >= IDC_EDIT1 + 9 && nID < IDC_EDIT1 + 26)*/
        if(nID == IDC_EDIT1 + 29)
        {
            DoEvent(hWnd, nID);
        }
        else
        {
            OnBtnCmd(hWnd, nID);
        }
    }
}
/*
 *
 */
static int BoardParamWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateBoardParamInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 200;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 200;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_board_param)
            {
                free(data.pwnd_board_param);
                SetWindowAdditionalData(hWnd, 0);
            }
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_BOARD_TEST]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_IDLE:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_board_param)
            {
                data.pwnd_board_param->pulse = GetBoardPulse(GetHosting(hWnd));
                HWND hTmp = GetDlgItem(hWnd, IDC_BOARD);
                char sc[64] = {0};
                const char *psc = GetWindowCaption(hTmp);
                short n = atoi(psc);
                if(n != data.pwnd_board_param->pulse)
                {
                    sprintf(sc, "%d", data.pwnd_board_param->pulse);
                    SetWindowCaption(hTmp, sc);
                    InvalidateRect(hTmp, NULL, TRUE);
                }
            }
        }
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateBoardParamWindow(HWND hHosting)
{
    WND_EXTRA_DATA data;
    data.pwnd_board_param = calloc(1, sizeof(WND_BOARD_PARAM));
    data.pwnd_board_param->pulse = GetBoardPulse(hHosting);
    int x = (CX_SCREEN - CX_BOARD_TEST_BMP) / 2;
    int y = (CY_SCREEN - CY_BOARD_TEST_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(527);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = BoardParamWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_BOARD_TEST_BMP;
    CreateInfo.by = y + CY_BOARD_TEST_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
